package com.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import metier.Imetier;
import model.Site;
import model.alexa.Analyse;
import model.alexa.AudienceGeographique_M;
import model.alexa.Keyword_M;
import model.alexa.UpstreamSites_M;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AudienceGeo extends ActionSupport {

	@Autowired
	private Imetier metier;

	private Long idSite;
	private int classment;
	private double fidelite;
	private double pagevue;
	private String moyentemps;
	private List<UpstreamSites_M> up = new ArrayList<UpstreamSites_M>();
	private List<AudienceGeographique_M> pays;
	private List<Keyword_M> keyword;
	private Site site;

	public String execute() {
		return Action.SUCCESS;
	}

	public int getClassment() {
		return classment;
	}

	public void setClassment(int classment) {
		this.classment = classment;
	}

	public double getFidelite() {
		return fidelite;
	}

	public void setFidelite(double fidelite) {
		this.fidelite = fidelite;
	}

	public double getPagevue() {
		return pagevue;
	}

	public void setPagevue(double pagevue) {
		this.pagevue = pagevue;
	}

	public String getMoyentemps() {
		return moyentemps;
	}

	public void setMoyentemps(String moyentemps) {
		this.moyentemps = moyentemps;
	}

	public List<UpstreamSites_M> getUp() {
		return up;
	}

	public void setUp(List<UpstreamSites_M> up) {
		this.up = up;
	}

	public Long getIdSite() {
		return idSite;
	}

	public void setIdSite(Long idSite) {
		this.idSite = idSite;
	}

	public List<AudienceGeographique_M> getPays() {
		return pays;
	}

	public void setPays(List<AudienceGeographique_M> pays) {
		this.pays = pays;
	}

	public void setMetier(Imetier metier) {
		this.metier = metier;
	}

	public List<Keyword_M> getKeyword() {
		return keyword;
	}

	public void setKeyword(List<Keyword_M> keyword) {
		this.keyword = keyword;
	}

	public String alexa() {
		if(this.idSite == null) {
			this.idSite = 1L;
		}
		this.site = this.metier.findSitebyId(this.idSite);
		List<Analyse> analyse = this.site.getP();
		this.pays = new ArrayList<AudienceGeographique_M>();
		for (AudienceGeographique_M audm : analyse.get(analyse.size() - 1)
				.getPaye())
			this.pays.add(audm);
		return Action.SUCCESS;
	}

	public String getAlexa() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put("idSite", this.idSite);
		this.site = this.metier.findSitebyId(this.idSite);
		metier.ajouterAudiance(site.getId_site());
		this.site = this.metier.findSitebyId(this.idSite);
		List<Analyse> analyse = this.site.getP();
		this.pays = new ArrayList<AudienceGeographique_M>();
		for (AudienceGeographique_M audm : analyse.get(analyse.size() - 1)
				.getPaye())
			this.pays.add(audm);

		this.up = new ArrayList<UpstreamSites_M>();
		for (UpstreamSites_M audm : analyse.get(analyse.size() - 1).getUp())
			this.up.add(audm);

		this.keyword = new ArrayList<Keyword_M>();

		for (Keyword_M key : analyse.get(analyse.size() - 1).getKeyword())
			this.keyword.add(key);

		this.classment = analyse.get(analyse.size() - 1).getClassment();
		this.fidelite = analyse.get(analyse.size() - 1).getFidelite();
		this.moyentemps = analyse.get(analyse.size() - 1).getMoyentemps();
		this.pagevue = analyse.get(analyse.size() - 1).getPagevue();
		return "success";
	}
}
